package cn.danger.com.fragment.schedule;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;

/**
 * Created by 俊 on 2017/1/3.
 */
public class RemindTimeFragment extends BackHandledFragment implements View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private RelativeLayout on_time_layout;
    private ImageView on_time_imageView;
    private RelativeLayout five_layout;
    private ImageView five_imageView;
    private RelativeLayout fifteen_layout;
    private ImageView fifteen_imageView;
    private RelativeLayout thirteen_layout;
    private ImageView thirteen_imageView;
    private RelativeLayout one_hour_layout;
    private ImageView one_hour_imageView;
    private RelativeLayout two_hour_layout;
    private ImageView two_hour_imageView;
    private String remind_time = "准时";
    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_remind_time_layout, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);

        on_time_layout = (RelativeLayout) view.findViewById(R.id.on_time_layout);
        on_time_imageView = (ImageView) view.findViewById(R.id.on_time_imageView);
        five_layout = (RelativeLayout) view.findViewById(R.id.five_layout);
        five_imageView = (ImageView) view.findViewById(R.id.five_imageView);
        fifteen_layout = (RelativeLayout) view.findViewById(R.id.fifteen_layout);
        fifteen_imageView = (ImageView) view.findViewById(R.id.fifteen_imageView);
        thirteen_layout = (RelativeLayout) view.findViewById(R.id.thirteen_layout);
        thirteen_imageView = (ImageView) view.findViewById(R.id.thirteen_imageView);
        one_hour_layout = (RelativeLayout) view.findViewById(R.id.one_hour_layout);
        one_hour_imageView = (ImageView) view.findViewById(R.id.one_hour_imageView);
        two_hour_layout = (RelativeLayout) view.findViewById(R.id.two_hour_layout);
        two_hour_imageView = (ImageView) view.findViewById(R.id.two_hour_imageView);

        Bundle bundle = getArguments();

        if (bundle != null) {
            remind_time = bundle.getString("index");
            reset();
            if (remind_time.equals("准时")) {
                on_time_imageView.setVisibility(View.VISIBLE);
            } else if (remind_time.equals("5分钟前")) {
                five_imageView.setVisibility(View.VISIBLE);
            } else if (remind_time.equals("15分钟前")) {
                fifteen_imageView.setVisibility(View.VISIBLE);
            } else if (remind_time.equals("30分钟前")) {
                thirteen_imageView.setVisibility(View.VISIBLE);
            } else if (remind_time.equals("一小时前")) {
                one_hour_imageView.setVisibility(View.VISIBLE);
            } else if (remind_time.equals("两小时前")) {
                two_hour_imageView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        on_time_layout.setOnClickListener(this);
        five_layout.setOnClickListener(this);
        fifteen_layout.setOnClickListener(this);
        thirteen_layout.setOnClickListener(this);
        one_hour_layout.setOnClickListener(this);
        two_hour_layout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        reset();
        switch (v.getId()) {
            case R.id.on_time_layout:
                on_time_imageView.setVisibility(View.VISIBLE);
                remind_time = "准时";
                break;
            case R.id.five_layout:
                five_imageView.setVisibility(View.VISIBLE);
                remind_time = "5分钟前";
                break;
            case R.id.fifteen_layout:
                fifteen_imageView.setVisibility(View.VISIBLE);
                remind_time = "15分钟前";
                break;
            case R.id.thirteen_layout:
                thirteen_imageView.setVisibility(View.VISIBLE);
                remind_time = "30分钟前";
                break;
            case R.id.one_hour_layout:
                one_hour_imageView.setVisibility(View.VISIBLE);
                remind_time = "一小时前";
                break;
            case R.id.two_hour_layout:
                two_hour_imageView.setVisibility(View.VISIBLE);
                remind_time = "两小时前";
                break;
            default:
                break;
        }
        back();
    }

    private void back() {
        if (handler != null) {
            Message message = Message.obtain();
            message.what = 1;
            message.obj = remind_time.toString();
            handler.sendMessage(message);
        }
        getFragmentManager().popBackStack();
    }

    private void reset() {
        on_time_imageView.setVisibility(View.GONE);
        five_imageView.setVisibility(View.GONE);
        fifteen_imageView.setVisibility(View.GONE);
        thirteen_imageView.setVisibility(View.GONE);
        one_hour_imageView.setVisibility(View.GONE);
        two_hour_imageView.setVisibility(View.GONE);
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }


}
